<?php

namespace app\admin\controller\general;

use app\common\controller\Backend;
use app\common\library\Email;
use app\admin\model\HeyPayConfig as ConfigModel;
use app\admin\model\HeyInfo;
use think\Exception;

/**
 * 支付报关参数配置
 *
 * @icon fa fa-cogs
 * @remark 可以在此增改系统的变量和分组,也可以自定义分组和变量,如果需要删除请从数据库中删除
 */
class Heypayconfig extends Backend
{

    /**
     * @var \app\common\model\HeyPayConfig
     */
    protected $model = null;
    protected $noNeedRight = [];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('HeyPayConfig');
    }

    /**
     * 查看
     */
    public function index()
    {
        $siteList = [];
        $groupList = ConfigModel::getGroupList();
        foreach ($groupList as $k => $v) {
            $siteList[$k]['name'] = $k;
            $siteList[$k]['title'] = $v;
            $siteList[$k]['list'] = [];
        }

        $hey_pay_config = HeyInfo::getPayConfig($this->auth->id);

        foreach ($this->model->all() as $_row) {
            if (!isset($siteList[$_row['group']])) {
                continue;
            }
            $temp_row = $_row->toArray();
            $temp_row['value'] = isset($hey_pay_config[$_row['group']][$_row['name']])?$hey_pay_config[$_row['group']][$_row['name']]:$temp_row['value'];
            $temp_row['title'] = __($temp_row['title']);
            if (in_array($temp_row['type'], ['select', 'selects', 'checkbox', 'radio'])) {
                $temp_row['value'] = explode(',', $temp_row['value']);
            }
            $temp_row['content'] = json_decode($temp_row['content'], TRUE);
            $siteList[$_row['group']]['list'][] = $temp_row;
        }
        $index = 0;
        foreach ($siteList as $k => &$v) {
            $v['active'] = !$index ? true : false;
            $index++;
        }
        $this->view->assign('siteList', $siteList);
        $this->view->assign('typeList', ConfigModel::getTypeList());
        $this->view->assign('groupList', ConfigModel::getGroupList());
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                foreach ($params as $k => &$v) {
                    $v = is_array($v) ? implode(',', $v) : $v;
                }
                try {
                    if (in_array($params['type'], ['select', 'selects', 'checkbox', 'radio', 'array'])) {
                        $params['content'] = json_encode(ConfigModel::decode($params['content']), JSON_UNESCAPED_UNICODE);
                    } else {
                        $params['content'] = '';
                    }
                    $result = $this->model->create($params);
                    if ($result !== false) {
                        try {
                            // $this->refreshFile();
                            $this->success();
                        } catch (Exception $e) {
                            $this->error($e->getMessage());
                        }
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     * @param null $ids
     */
    public function edit($ids = NULL)
    {
        if ($this->request->isPost()) {
            
            $hey_pay_config = HeyInfo::getPayConfig($this->auth->id);

            $row = $this->request->post("row/a");
            if ($row) {
                $config_list = [];
                foreach ($this->model->all() as $v) {
                    if (isset($row[$v['name']])) {
                        $value = $row[$v['name']];
                        if (is_array($value) && isset($value['field'])) {
                            $value = json_encode(ConfigModel::getArrayData($value), JSON_UNESCAPED_UNICODE);
                        } else {
                            $value = is_array($value) ? implode(',', $value) : $value;
                        }
                        $v['value'] = $value;
                        $config_list[$v['group']][$v['name']] = $v['value'];
                        // $config_list[] = $v->toArray();
                    }
                }
                try {
                    if ($hey_pay_config) {
                        $config_list = array_replace_recursive($hey_pay_config,$config_list);
                        // var_dump($config_list);exit;
                        $config_list = json_encode($config_list,JSON_UNESCAPED_UNICODE);
                        $flag = model('HeyInfo')->isUpdate(true)->save(['pay_config'=>$config_list],['admin_id'=>$this->auth->id]);
                    }else{
                        $config_list = json_encode($config_list,JSON_UNESCAPED_UNICODE);
                        $flag = model('HeyInfo')->isUpdate(true)->save(['pay_config'=>$config_list,'admin_id'=>$this->auth->id],['admin_id'=>$this->auth->id]);
                    }
                    // $this->model->allowField(true)->saveAll($config_list);
                    // $this->refreshFile();
                    $this->success();
                } catch (Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
    }
}
